

function say() {
    alert("LC:74. 搜索二维矩阵");
}
say();


/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function(matrix, target) {
    //6:36-50 // 9:20-35
    var rowCount = matrix.length
    var colCount = matrix[0].length
    if(matrix[0][0] > target || matrix[rowCount - 1][colCount - 1] < target){
        return false
    }

    //找到行
    var targetRow = 0
    for(var i = 0; i < rowCount -1; i++){
        if(matrix[i][0] == target ||
        matrix[i + 1][0] == target ||
        matrix[i + 1][colCount - 1] == target){
            return true
        }
        if(matrix[i][0] < target && matrix[i + 1][0] > target){
            targetRow = i
            break
        }
        if(matrix[i + 1][0] < target && matrix[i + 1][colCount - 1] > target){
            targetRow = i + 1
            break
        }
    }

    var item = matrix[targetRow]
    for(var i = 0; i < colCount; i++){
        if(item[i] == target){
            return true
        }
    }

    return false
};

